from flask import request, jsonify
from flask_app import app
import joblib
import pandas as pd
import yaml

def load_config(lottery):
    config_map = {
        "ssq": "config/ssq_config.yaml",
        "zc": "config/zc_config.yaml",
        "qxc": "config/qxc_config.yaml"
    }
    with open(config_map[lottery], 'r') as f:
        return yaml.safe_load(f)

def load_model(config):
    return joblib.load(config['model_path'])

def load_features(config):
    return pd.read_csv(config['features_data_path'])

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json
    lottery = data.get('lottery')
    if lottery not in ['ssq', 'zc', 'qxc']:
        return jsonify({"error": "Invalid lottery type"}), 400

    config = load_config(lottery)
    model = load_model(config)
    features = load_features(config)
    
    predictions = model.predict(features)
    return jsonify({"predictions": predictions.tolist()})

@app.route('/')
def home():
    return "Welcome to the Lottery Prediction API!"